package com.zx.ishopping.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zx.ishopping.Constants.DeleteConstant;
import com.zx.ishopping.Constants.InsertConstant;
import com.zx.ishopping.DTO.CategoryDTO;
import com.zx.ishopping.DTO.CategoryPageQueryDTO;
import com.zx.ishopping.entity.Category;
import com.zx.ishopping.entity.Products;
import com.zx.ishopping.exceptions.CategoryFailException;
import com.zx.ishopping.mapper.CategoryMapper;
import com.zx.ishopping.mapper.ProductMapper;
import com.zx.ishopping.results.PageResult;
import com.zx.ishopping.services.CategoryService;
import com.zx.ishopping.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    
    @Autowired
    private ProductMapper productMapper;

    /**
     * 获取分类信息列表
     * @return
     */
    @Override
    public List<Category> getCategoryList() {
        List<Category> categoryList = categoryMapper.getCategoryList();
        return categoryList;
    }

    /**
     * 插入分类信息
     * @param categoryDTO
     */
    @Override
    public void insertCategory(CategoryDTO categoryDTO) {
        
        
        Category categoryByName = categoryMapper.getCategoryByName(categoryDTO.getName());
        if (categoryByName != null){
            throw new CategoryFailException(InsertConstant.EXIST_CATEGORY);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        Long o = ThreadLocalUtil.get();
        category.setCreateUser(o);
        categoryMapper.insertCategory(category);
    }

    /**
     * 分页查询分类
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> result = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setData(result);
        return pageResult;
    }

    /**
     * 根据id删除分类
     * @param ids
     */
    @Override
    public void deleteCategory(List<Long> ids) {
        ///判断该分类下是否存在商品
        for (Long id : ids) {
            Long productNumByCategoryId = productMapper.getProductNumByCategoryId(id);
            if (productNumByCategoryId > 0){
                throw new CategoryFailException(DeleteConstant.EXIST_PRODUCT);
            }
        }
        //批量删除分类
        categoryMapper.deleteCategory(ids);
    }

    /**
     * 更新分类信息
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        //根据分类名查询分类，判断分类名是否已存在
        Category categoryById = categoryMapper.getCategoryById(categoryDTO.getId());
        if (!categoryDTO.getName().equals(categoryById.getName())){
            Category categoryByName = categoryMapper.getCategoryByName(categoryDTO.getName());
            if (categoryByName != null){
                throw new CategoryFailException(DeleteConstant.EXIST_CATEGORY);
            }
        }
       
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.updateCategory(category);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @Override
    public Category getCategoryById(Long id) {
        Category categoryById = categoryMapper.getCategoryById(id);
        return categoryById;
    }
}
